#include "EWAUIWidgetTest1.hpp"

#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"

#include "EWAUIWidget/EWAUIWidgetHeader.hpp"

namespace Eyer
{
    EWAUIWidgetTest1::EWAUIWidgetTest1()
        : parentLayout(EWAUILayoutParams::MATCH_PARENT, EWAUILayoutParams::MATCH_PARENT)
    {
        Eyer::EyerSmartPtr<EWAUIWidgetButton> button1(new EWAUIWidgetButton(EWAUILayoutParams::WRAP_CONTENT, EWAUILayoutParams::WRAP_CONTENT));
        parentLayout.AddWidget(button1);
        Eyer::EyerSmartPtr<EWAUIWidgetButton> button2(new EWAUIWidgetButton(EWAUILayoutParams::MATCH_PARENT, EWAUILayoutParams::WRAP_CONTENT));
        parentLayout.AddWidget(button2);
    }

    EWAUIWidgetTest1::~EWAUIWidgetTest1()
    {

    }

    int EWAUIWidgetTest1::OnGUI()
    {
        ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
        ImGui::Begin(u8"Time Line 时间轴");
        ImGui::PopStyleVar();

        ImVec2 XY = ImGui::GetCursorScreenPos();
        ImVec2 WH = ImGui::GetContentRegionAvail();

        ImVec2 mousePos = ImGui::GetMousePos();
        ImVec2 viewPos(mousePos.x - XY.x, mousePos.y - XY.y);

        ImDrawList* draw_list = ImGui::GetWindowDrawList();

        /**************************************************/

        parentLayout.OnMeasure(WH.x, WH.y);
        parentLayout.Draw();

        int graphSize = parentLayout.GetUIGraphSize();
        for(int i=0;i<graphSize;i++){
            EyerSmartPtr<EWAUIGraph> g = parentLayout.GetUIGraph(i);

            if(g->GetType() == EWAUIGraphType::RECT){
                EyerSmartPtr<EWAUIGraphRect> gRect = g;

                auto lineColor = IM_COL32(255, 0, 0, 255);
                auto graphColor = IM_COL32(255, 255, 255, 255);

                Vec2I offsetXY(XY.x, XY.y);
                Vec2I xy = gRect->xy + offsetXY;
                Vec2I wh = gRect->wh;

                ImVec2 pA(xy.x(), xy.y());
                ImVec2 pB(xy.x() + wh.x(), xy.y() + wh.y());

                draw_list->AddRectFilled(pA, pB, graphColor, 0);
                draw_list->AddRect(pA, pB, lineColor, 0);
            }
        }
        /**************************************************/

        ImGui::End();

        return 0;
    }
}